// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse the JSON, add this file to your project and do:
//
//   let realResult = try RealResult(json)

import Foundation

/// 实时成绩
// MARK: - RealResult
struct RealResult: Codable {
    /// 头部数据
    let head: Head
    /// 主体数据
    let body: Body

    enum CodingKeys: String, CodingKey {
        case head = "Head"
        case body = "Body"
    }
}

// MARK: RealResult convenience initializers and mutators

extension RealResult {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(RealResult.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        head: Head? = nil,
        body: Body? = nil
    ) -> RealResult {
        return RealResult(
            head: head ?? self.head,
            body: body ?? self.body
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

/// 主体数据
// MARK: - Body
struct Body: Codable {
    /// 代表队/代表团/俱乐部
    let team: String
    /// 运动员姓名
    let athleteName: String
    /// 运动员号码
    let athleteBib: String
    /// 运动员联系方式
    let athleteTelephone: String
    /// A线成绩
    let resultA: String
    /// B线成绩
    let resultB: String
    /// 排名
    let rank: String

    enum CodingKeys: String, CodingKey {
        case team = "Team"
        case athleteName = "AthleteName"
        case athleteBib = "AthleteBib"
        case athleteTelephone = "AthleteTelephone"
        case resultA = "ResultA"
        case resultB = "ResultB"
        case rank = "Rank"
    }
}

// MARK: Body convenience initializers and mutators

extension Body {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(Body.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        team: String? = nil,
        athleteName: String? = nil,
        athleteBib: String? = nil,
        athleteTelephone: String? = nil,
        resultA: String? = nil,
        resultB: String? = nil,
        rank: String? = nil
    ) -> Body {
        return Body(
            team: team ?? self.team,
            athleteName: athleteName ?? self.athleteName,
            athleteBib: athleteBib ?? self.athleteBib,
            athleteTelephone: athleteTelephone ?? self.athleteTelephone,
            resultA: resultA ?? self.resultA,
            resultB: resultB ?? self.resultB,
            rank: rank ?? self.rank
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

/// 头部数据
// MARK: - Head
struct Head: Codable {
    /// 设备Id
    let deviceID: String
    /// 消息Uuid
    let messageUUID: String
    /// 发送消息的时间。格式：yyyy-MM-dd HH:mm:ss.fff 比如：2020-09-02 12:24:09.923
    let timestamp: String
    /// 消息类型
    let messageType: String

    enum CodingKeys: String, CodingKey {
        case deviceID = "DeviceId"
        case messageUUID = "MessageUuid"
        case timestamp = "Timestamp"
        case messageType = "MessageType"
    }
}

// MARK: Head convenience initializers and mutators

extension Head {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(Head.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        deviceID: String? = nil,
        messageUUID: String? = nil,
        timestamp: String? = nil,
        messageType: String? = nil
    ) -> Head {
        return Head(
            deviceID: deviceID ?? self.deviceID,
            messageUUID: messageUUID ?? self.messageUUID,
            timestamp: timestamp ?? self.timestamp,
            messageType: messageType ?? self.messageType
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

// MARK: - Helper functions for creating encoders and decoders

func newJSONDecoder() -> JSONDecoder {
    let decoder = JSONDecoder()
    if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
        decoder.dateDecodingStrategy = .iso8601
    }
    return decoder
}

func newJSONEncoder() -> JSONEncoder {
    let encoder = JSONEncoder()
    if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
        encoder.dateEncodingStrategy = .iso8601
    }
    return encoder
}
